/* Name : main.c * Purpose : Source code for KEYPAD Interfacing with AT89C52. * Author : Gemicates * Date : 2014-01-19 * Website : www.gemicates.org * Revision : None */ #include <REGX52.H> // header file for AT89c52 series #define keypad P0 #define Lcd_port P2 // GPIO direction register declaration sbit rs = P3^7; // register select pin sbit rw = P3^6; // read write pin sbit en = P3^5; // enable pin // Row and Column pins declaration sbit R1 = keypad^0; sbit R2 = keypad^1; sbit R3 = keypad^2; sbit R4 = keypad^3; sbit C1 = keypad^4; sbit C2 = keypad^5; sbit C3 = keypad^6; // Function declarations void delay(unsigned int count); void lcdcmd_address(unsigned char cmd); void lcddata(unsigned char send_data); void lcd_string(unsigned char str[10]); void lcd_data_string(unsigned char *str); void check_column_one(); void check_column_two(); void check_column_three(); void output(int key); void main() // main function { lcdcmd_address(0x38); // for using 8-bit 2 row mode and 5x7 Dots of LCD lcdcmd_address(0x0E); // turn display ON for cursor blinking lcdcmd_address(0x01); // clear screen lcdcmd_address(0x06); // display ON delay(10); lcdcmd_address(0x86); // bring cursor to position 6 of ROW 1 lcddata('H'); lcdcmd_address(0x87); lcddata('I'); lcddata('!'); delay(10); lcdcmd_address(0xC3); // bring cursor to position 3 of ROW 2 lcd_string("**GUYS**"); delay(50); lcdcmd_address(0x01); // clear screen lcdcmd_address(0x83); // bring cursor to position 3 of ROW 1 lcd_string("WELCOME TO"); delay(100); lcdcmd_address(0xC3); // bring cursor to position 3 of ROW 2 lcd_string("GEMICATES"); lcdcmd_address(0x01); // clear screen lcdcmd_address(0x0C); // Display On cursor Off C1=C2=C3=1; // The output of all the columns will be high while(1) { R1=R2=R3=R4=0; // made all the rows zero if(C1==0) check_column_one(); // check pressed key is a column one? else if(C2==0) check_column_two(); // check pressed key is a column two? else if(C3==0) check_column_three(); // check pressed key is a column three? } } void delay(unsigned int count) { int m,n; for(m=0;m<count;m++) for(n=0;n<1275;n++); } void lcdcmd_address(unsigned char cmd) // Function to send command to LCD { Lcd_port = cmd; rs= 0; rw= 0; en= 1; delay(5); en= 0; return; } void lcddata(unsigned char send_data) // Function to send data to LCD { Lcd_port = send_data; rs= 1; rw=0; en=1; delay(5); en=0; return; } void lcd_string(unsigned char str[10]) // Funtion to Initialize LCD { lcd_data_string(str); } void lcd_data_string(unsigned char *str) // Function to send string on LCD { int i=0; while(str[i]!='\0') { lcddata(str[i]); i++; delay(10); } return; } void check_column_one() // Detecting column one? { R1=R2=R3=R4=1; // After detecting column All rows set to high R1=0; if((R1==0)&&(C1==0)) output(1); R1=1; R2=0; if((R2==0)&&(C1==0)) output(4); R2=1; R3=0; if((R3==0)&&(C1==0)) output(7); R3=1; R4=0; if((R4==0)&&(C1==0)) output(10); R4=1; } void check_column_two() // Detecting column two? { R1=R2=R3=R4=1; // After detecting column All rows set to high R1=0; if((R1==0)&&(C2==0)) output(2); R1=1; R2=0; if((R2==0)&&(C2==0)) output(5); R2=1; R3=0; if((R3==0)&&(C2==0)) output(8); R3=1; R4=0; if((R4==0)&&(C2==0)) output(11); R4=1; } void check_column_three() // Detecting column three? { R1=R2=R3=R4=1; // After detecting column All rows set to high R1=0; if((R1==0)&&(C3==0)) output(3); R1=1; R2=0; if((R2==0)&&(C3==0)) output(6); R2=1; R3=0; if((R3==0)&&(C3==0)) output(9); R3=1; R4=0; if((R4==0)&&(C3==0)) output(12); R4=1; } void output(int key) { lcdcmd_address(0x01); // clear screen lcdcmd_address(0x86); // bring cursor to position 3 of ROW 1 switch(key) { case 1: lcd_string("ONE"); break; case 2: lcd_string("TWO"); break; case 3: lcd_string("THREE"); break; case 4: lcd_string("FOUR"); break; case 5: lcd_string("FIVE"); break; case 6: lcd_string("SIX"); break; case 7: lcd_string("SEVEN"); break; case 8: lcd_string("EIGHT"); break; case 9: lcd_string("NINE"); break; case 10: lcd_string("TEN"); break; case 11: lcd_string("ELEVEN"); break; case 12: lcd_string("TWELVE"); break; } }